﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Data;
using LotteryVote.Entity;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic
{
    /// <summary>
    /// 游戏玩法业务处理类
    /// </summary>
    public class GamePlayWayFacade
    {
        private GameTypeDataAccess _daGameType;
        private PlayWayDataAccess _daPlayWay;
        private GamePlayWayDataAccess _daGamePlayWay;
        private CompanyTypeFacade _comTypeFacade;

        internal GameTypeDataAccess DaGameType
        {
            get
            {
                if (_daGameType == null)
                    _daGameType = new GameTypeDataAccess();
                return _daGameType;
            }
            set
            {
                _daGameType = value;
            }
        }
        internal PlayWayDataAccess DaPlayWay
        {
            get
            {
                if (_daPlayWay == null)
                    _daPlayWay = new PlayWayDataAccess();
                return _daPlayWay;
            }
            set
            {
                _daPlayWay = value;
            }
        }
        internal GamePlayWayDataAccess DaGamePlayWay
        {
            get
            {
                if (_daGamePlayWay == null)
                    _daGamePlayWay = new GamePlayWayDataAccess();
                return _daGamePlayWay;
            }
            set
            {
                _daGamePlayWay = value;
            }
        }
        public CompanyTypeFacade CompanyTypeFacade
        {
            get
            {
                if (_comTypeFacade == null)
                    _comTypeFacade = new CompanyTypeFacade();
                return _comTypeFacade;
            }
            set
            {
                _comTypeFacade = value;
            }
        }

        #region GameType
        public void AddGameType(string gameName, string description)
        {
            if (string.IsNullOrEmpty(gameName))
                throw new ArgumentNullException("typeName");
            GameType gameType = new GameType
            {
                GameName = gameName,
                Description = description
            };
            DaGameType.InsertGameType(gameType);
        }
        public void UpdateGameType(int gameId, string gameName, string description)
        {
            if (string.IsNullOrEmpty(gameName))
                throw new ArgumentNullException(gameName);
            var gameType = DaGameType.GetGameType(gameId);
            if (gameType == null)
                throw new InvalidDataException("gameType,gameId:" + gameId);
            gameType.GameName = gameName;
            gameType.Description = description;
            DaGameType.UpdateGameType(gameType);
        }
        public void RemoveGameType(int gameId)
        {
            DaGameType.DeleteGameType(gameId);
        }
        #endregion

        #region PlayWay
        public void AddPlayWay(string wayName, string description)
        {
            if (string.IsNullOrEmpty(wayName))
                throw new ArgumentNullException("wayName");
            if (DaPlayWay.GetPlayWay(wayName) != null)
                return;
            PlayWay playWay = new PlayWay
            {
                WayName = wayName,
                Description = description
            };
            DaPlayWay.InsertPlayWay(playWay);
        }
        public void UpdatePlayWay(int wayId, string wayName, string description)
        {
            if (string.IsNullOrEmpty(wayName))
                throw new ArgumentNullException("wayName");
            var playWay = DaPlayWay.GetPlayWay(wayId);
            if (playWay == null)
                throw new InvalidDataException("playWay,wayId:" + wayId);
            playWay.WayName = wayName;
            playWay.Description = description;
            DaPlayWay.UpdatePlayWay(playWay);
        }
        public void RemovePlayWay(int wayId)
        {
            DaPlayWay.DeletePlayWay(wayId);
        }
        #endregion

        /// <summary>
        ///根据GameId和WayId添加新的游戏玩法 .
        /// </summary>
        /// <param name="gameId">The game id.</param>
        /// <param name="wayId">The way id.</param>
        public void AddGamePlayWay(int gameId, int wayId)
        {
            var gameType = DaGameType.GetGameType(gameId);
            if (gameType == null)
                throw new InvalidDataException("GameType,gameId:" + gameId);
            var playWay = DaPlayWay.GetPlayWay(wayId);
            if (playWay == null)
                playWay = new PlayWay();
            GamePlayWay gpy = new GamePlayWay
            {
                GameType = gameType,
                PlayWay = playWay
            };
            DaGamePlayWay.InsertGamePlayWay(gpy);
        }
        /// <summary>
        /// 移除游戏玩法.
        /// </summary>
        /// <param name="gameId">游戏类型Id.</param>
        /// <param name="wayId">玩法Id.</param>
        public void RemoveGamePlayWay(int gameId, int wayId)
        {
            DaGamePlayWay.DeleteGamePlayWay(gameId, wayId);
        }
        /// <summary>
        /// 移除游戏玩法.
        /// </summary>
        /// <param name="id">游戏玩法Id.</param>
        public void RemoveGamePlayWay(int id)
        {
            DaGamePlayWay.DeleteGamePlayWay(id);
        }
        public bool IsExistGamePlayWay(int gameId, int wayId)
        {
            return DaGamePlayWay.IsExistGamePlayWay(gameId, wayId);
        }

        public void AddCompanyTypeSupportWayOfPlay(int companyTypeId, int gamePlayWayId)
        {
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            var gamePlayWay = DaGamePlayWay.GetGamePlayWay(gamePlayWayId);
            if (gamePlayWay == null)
                throw new InvalidDataException("gamePlayWay,gamePlayWayId:" + gamePlayWayId);
            DaGamePlayWay.InsertCompanyTypeSupportWayOfPlay(new CompanyTypeSupportWayOfPlay { CompanyType = companyType, GamePlayWay = gamePlayWay });
        }
        public void RemoveCompanyTypeSupportWayOfPlay(int companyTypeId, int gamePlayWayId)
        {
            DaGamePlayWay.DeleteCompanyTypeSupportWayOfPlay(companyTypeId, gamePlayWayId);
        }
        public bool IsCompanyTypeSupportWayOfPlay(int companyTypeId, int gamePlayWayId)
        {
            return DaGamePlayWay.IsCompanyTypeSupportWayOfPlay(companyTypeId, gamePlayWayId);
        }

        public IList<PlayWay> GetAllPlayWay()
        {
            return DaPlayWay.GetAllPlayWay();
        }
        public IList<GameType> GetAllGameType()
        {
            return DaGameType.GetAllGameType();
        }
        /// <summary>
        /// 获取所有游戏玩法，并按游戏类型分组.
        /// </summary>
        /// <returns></returns>
        public IDictionary<GameType, IList<PlayWay>> GetAllGamePlayWayGroupByGameType()
        {
            var gamePlayWayList = DaGamePlayWay.GetAllGamePlayWay();
            Dictionary<GameType, IList<PlayWay>> dic = new Dictionary<GameType, IList<PlayWay>>();
            foreach (var item in gamePlayWayList)
            {
                var key = dic.Keys.Where(it => it.GameId == item.GameType.GameId).SingleOrDefault();
                if (key != null)
                    dic[key].Add(item.PlayWay);
                else
                    dic.Add(item.GameType, new List<PlayWay> { item.PlayWay });
            }
            return dic;
        }
        public IEnumerable<PlayWay> GetPlayWayByGameType(int gameId)
        {
            var gameType = (GameTypeEnum)gameId;
            return DaGamePlayWay.GetGamePlayWayByGameType(gameType).Select(it => it.PlayWay);
        }
        public IList<GamePlayWay> GetAllGamePlayWay()
        {
            return DaGamePlayWay.GetAllGamePlayWay();
        }
        public IDictionary<GameType, IList<PlayWay>> GetGamePlayWayByCompanyType(int companyTypeId)
        {
            var companyType = CompanyTypeFacade.GetCompanyType(companyTypeId);
            if (companyType == null)
                throw new InvalidDataException("companyType,companyTypeId:" + companyTypeId);
            var gamePlayWayList = DaGamePlayWay.GetGamePlayWayByCompanyType(companyType);
            Dictionary<GameType, IList<PlayWay>> dic = new Dictionary<GameType, IList<PlayWay>>();
            foreach (var item in gamePlayWayList)
            {
                var key = dic.Keys.Where(it => it.GameId == item.GameType.GameId).SingleOrDefault();
                if (key != null)
                    dic[key].Add(item.PlayWay);
                else
                    dic.Add(item.GameType, new List<PlayWay> { item.PlayWay });
            }
            return dic;
        }
    }
}
